package com.patrol.logisticsmanage.ui.appear;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.patrol.logisticsmanage.R;
import com.patrol.logisticsmanage.common.InputFilterMinMax;
import com.patrol.logisticsmanage.net.BaseObserver;
import com.patrol.logisticsmanage.net.BaseReponse;
import com.patrol.logisticsmanage.net.BaseRequest;
import com.patrol.logisticsmanage.ui.base.BaseActivity;
import com.patrol.logisticsmanage.ui.canteen.utils.ToastUtils;
import com.patrol.logisticsmanage.ui.equip.StudentInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

@Route(path = "/app/EditInfoActivity")
public class EditInfoActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_height)
    EditText etHeight;
    @BindView(R.id.et_weight)
    EditText etWeight;
    @BindView(R.id.sp_type)
    Spinner spType;
    @BindView(R.id.tv_calculate)
    TextView tvCalculate;

    @Autowired
    String url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_info);
        ButterKnife.bind(this);
        InputFilterMinMax.setRegin(etHeight,100,200);
        InputFilterMinMax.setRegin(etWeight,20,200);
        tvTitle.setText("营养分析");
        getInfo();

    }

    private void getInfo() {
        BaseRequest.getInstance().getApiServise().getStuInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<StudentInfo>(this.mContext) {
                    @Override
                    public void onSuccess(BaseReponse<StudentInfo> baseReponse) {
                        StudentInfo info = baseReponse.getResults();
                        if(null != info){
                            etHeight.setText(info.getSheng());
                            etWeight.setText(info.getTiz());
                        }
                    }

                    @Override
                    public void onCodeError(BaseReponse tBaseReponse) {
                    }

                    @Override
                    public void onFailure(Throwable e, boolean netWork) throws Exception {

                    }
                });
    }

    @OnClick(R.id.tv_calculate)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etHeight.getText())) {
            ToastUtils.showToast("身高不能为空");
            return;
        }
        if (TextUtils.isEmpty(etWeight.getText())) {
            ToastUtils.showToast("体重不能为空");
            return;
        }
        String bmi = "您的BMI是:--";
        try {
            bmi = bmi();
        }catch (Exception ex){
            ToastUtils.showToast("输入格式不正确");
            return;
        }
        ARouter.getInstance().build("/app/tuijian").withString("bmi",bmi).navigation();
    }

    private String bmi(){
        String str = "您的BMI是:";
        double x = 0d;// Double.parseDouble(etHeight.getText().toString());//身高
        double y = 0d;//Double.parseDouble(etWeight.getText().toString());//体重//体重除以身高的平方
        try{
            x = Double.parseDouble(etHeight.getText().toString());//身高
            y = Double.parseDouble(etWeight.getText().toString());//体重//体重除以身高的平方
        }catch (Exception ex){
            throw ex;
        }
        double res;
        if(x<=0 || y<=0) {
            return str;
        }
        x = x/100;
        res = y / (x*x);
        String str1 = String.format("%.2f",res);
        str = str + str1;

        str +=   " 体型:";
        if(res < 18.4)
            str += "偏瘦";
        else if(res < 24)
            str += "正常";
        else if(res<28)
            str += "过重";
        else
            str += "肥胖";

        return str;
    }
}
